<?php
/*
 * Created on 2008-1-24
 *
 * This is an open source software, please obey the GPL
 * The Referee Service is offered by Cacomand Inc. 
 * View Http://www.cacomand.com for support or more information
 * You can also view distrema.googlecode.com to join the community.
 */
include("lib_user.php");
include("ccr_config.php");
$developer=true;
function local_user_login($username,$password)
{
	global $ccr_public_username;
	global $ccr_public_key;
	global $developer;
	global $epsn,$ccr_host,$ccr_basename;
	$ccr_link=mysql_connect($ccr_host,$ccr_public_username,$ccr_public_key);
	mysql_select_db($ccr_basename,$ccr_link);
	if($ccr_link!=FALSE)
	{
		$select_format='SELECT user_ID FROM user_list WHERE username="%s"';
		$query=sprintf($select_format,$username);
		if($developer)
			echo("".$query."<br />");
		$res_user_ID=mysql_query($query,$ccr_link);
		if($obj_user_ID=mysql_fetch_object($res_user_ID))
		{
			$test_link=$ccr_link=mysql_connect($ccr_host,$ccr_public_username,$ccr_public_key);
			if($test_link!=false)
			{
				setcookie("DistRema_epsn",$epsn, time() + (3600*24));
				setcookie("DistRema_user_ID",$obj->user_ID, time() + (3600*24)); 
				echo("您好".$username.", 欢迎您使用DistRema平台。<BR />");
				echo("如果您想注销，请点击<a href=user_logout.php>这里</a><BR />");
				return true;
			}
			else
			{
				echo("对不起,您输入的密码有误");
			}
		}
		else
		{
			echo("对不起,您输入的用户名不存在");
		}
		mysql_close($ccr_link);
	}
}		
	class ccr_action
	//this class is to use to clear the user base
	{
		function ccr_action($ccr_power_username,$ccr_power_password)
		{
			echo("CCR对象成功建立");
			$this->ccr_power_username=$ccr_power_username;
			$this->ccr_power_password=$ccr_power_password;
		}
		
		function ccr_reg()
 		{
 			global $ref_host;
			global $ref_username;
			global $ref_password;
			global $epsn;
			global $ccr_host;
			global $developer;
			$this->ccr_host=$ccr_host;
			$this->epsn=$epsn;
			$ref_link=mysql_connect($ref_host,$ref_username,$ref_password);
			mysql_select_db("referee_base",$ref_link);	
			if ($ref_link!=FALSE)
			{
				$select_format='SELECT * FROM epsn_pool WHERE epsn="%s"';
				$query=sprintf($select_format,$this->epsn);
				if($developer)
				print($query."<br />");
				$res_search_epsn=mysql_query($query,$ref_link);
				if(!mysql_fetch_object($res_search_epsn))
				{
					$insert_format='INSERT INTO epsn_pool (epsn,IP_address) VALUE ("%s","%s")';
					$query=sprintf($insert_format,$this->epsn,$this->ccr_host);
					if($developer)
					print($query."<br />");
					$res_nouse=mysql_query($query,$ref_link);
					if($res_nouse)
					echo("REF:CCR注册成功");
					else
					echo("REF:CCR注册失败");
					mysql_close($ref_link);
				}
				else
				{
					$update_format='UPDATE epsn_pool SET IP_address="%s" WHERE epsn="%s"';
					$query=sprintf($update_format,$this->ccr_host,$this->epsn);
					if($developer)
					print($query."<br />");
					$res_nouse=mysql_query($query,$ref_link);
					echo("epsn更新成功");
				}
			}
 		}
		
		function user_register($username,$password)
		{
			global $ccr_host;
			global $developer,$ccr_basename;
			$ccr_link=mysql_connect($ccr_host,$this->ccr_power_username,$this->ccr_power_password);
			mysql_select_db($ccr_basename,$ccr_link);
			if($ccr_link!=FALSE)
			{
				$insert_format='INSERT INTO user_list (username,password) VALUE ("%s","%s")';
				$query=sprintf($insert_format,$username,md5($password));
				if($developer)
				print($query."<br />");
				$res_insert_ID=mysql_query($query,$ccr_link);
				if($user_ID=mysql_insert_ID($ccr_link))
				{
					
					$query_format='CREATE TABLE `variables_%d` (`rec_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,`var_ID` INT UNSIGNED NOT NULL ,`caption` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL ,`memo` VARCHAR( 500 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci;;';
					$query=sprintf($query_format,$user_ID);
					if($developer)
						print($query."<br />");
					$res_nouse=mysql_query($query,$ccr_link);
					/*
					$query_format="CREATE USER '%s' IDENTIFIED BY '%s';";
					$query=sprintf($query_format,$username,$password);
					if($developer)
					print($query."<br />");
					$res_nouse=mysql_query($query,$ccr_link);
					
					$query_format="GRANT ALL PRIVILEGES ON `user_base`.`variables_%d` TO '%s' IDENTIFIED BY '%s';";
					$query=sprintf($query_format,$user_ID,$username,$password);
					if($developer)
						print($query."<br />");
					$res_nouse=mysql_query($query,$ccr_link);
					
					$query_format="GRANT ALL PRIVILEGES ON `user_base`.`sent_data_pool` TO '%s' IDENTIFIED BY '%s';";
					$query=sprintf($query_format,$username,$password);
					if($developer)
					print($query."<br />");
					$res_nouse=mysql_query($query,$ccr_link);
					*/
					return true;
				}
				else
				{
					echo("登录名已存在,请使用其他名称。");
					return false;
				}
				mysql_close($ccr_link);
			}
			else
				return false;
		}
		function user_delete()
		{
			global $ccr_host;
			global $developer,$ccr_basename;
			$ccr_link=mysql_connect($ccr_host,$this->ccr_power_username,$this->ccr_power_password);
			mysql_select_db($ccr_basename,$ccr_link);
			if($ccr_link!=FALSE)
			{
				//TODO:删除一个用户的数据表和user_list
			}
		}
	}
?>
